package org.acme.job.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.acme.job.po.SysJob;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface SysJobMapper extends BaseMapper<SysJob> {

    @Select(value = "<script>" + " select DISTINCT server_id from sys_job where status = 0 and server_id not in ( " + " <foreach collection='ids' separator=',' item='id'>" + " #{id} " + " </foreach> " + " )</script>")
    public List<Long> getDeathServerId(List<Long> ids);

    @Update("update sys_job set server_id = #{targetServerId} where server_id =#{sourceServerId} and `status` = 0 and id % #{total} = #{index} ")
    public void updateJobServerId(@Param("sourceServerId") Long sourceServerId, @Param("targetServerId") Long targetServerId, @Param("index") int index, @Param("total") int total);

    @Select(value = "select * from sys_job where status <> 1 and server_id =#{serverId} and ( retry_size = 0 or (retry_size < #{maxRetrySize} and now() > retry_time)) LIMIT #{maxLimitSize}")
    public List<SysJob> getJobList(@Param("serverId") Long serverId, @Param("maxRetrySize") int maxRetrySize, @Param("maxLimitSize") int maxLimitSize);
}
